package com.yurqi.dialog

import android.app.Dialog
import android.content.ComponentCallbacks
import android.content.Context
import android.os.Bundle
import android.view.*
import android.widget.*
import android.widget.CheckBox
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.ug_project.views.*
import me.ezitku.base.R

/**
 * Created by yurqi on 3/12/2017.
 */

open class SDialog : Dialog {
    constructor(context: Context, resId: Int) : super(context) {
        this.resId = resId
        rootView = LayoutInflater.from(this.context).inflate(resId, null)
    }

    constructor(context: Context, resId: Int, style: Int) : super(context, style) {
        this.resId = resId
        rootView = LayoutInflater.from(this.context).inflate(resId, null)
    }

    internal var resId: Int = 0
    val match_parent = ViewGroup.LayoutParams.MATCH_PARENT
    val wrap_content = ViewGroup.LayoutParams.WRAP_CONTENT
    var width = ViewGroup.LayoutParams.WRAP_CONTENT
    var height = ViewGroup.LayoutParams.WRAP_CONTENT
    var rootView: View
    var isTouchCancel = false
    var canCancel = true
    var backgroundDim = 0.5f
    var gravity = Gravity.CENTER

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(rootView)
        val _window = this.window
        window.setGravity(gravity)
        val layoutParams = window.attributes

        layoutParams.width = width
        layoutParams.height = height
        window.setDimAmount(backgroundDim)
        window.attributes = layoutParams
        setCanceledOnTouchOutside(isTouchCancel)
        setCancelable(canCancel)

//        when(rootView){
//            is ViewGroup->{
//                (rootView as ViewGroup).font = FONT_NAME
//            }
//            is TextView->{
//                (rootView as TextView).font = FONT_NAME
//            }
//        }
    }

    //findViewById
    fun button(resId: Int): android.widget.Button = rootView.findButton(resId)
    fun linearLayout(resId: Int): android.widget.LinearLayout = rootView.findLinearLayout(resId)
    fun relativeLayout(resId: Int): android.widget.RelativeLayout =
        rootView.findRelativeLayout(resId)

    fun imageView(resId: Int): android.widget.ImageView = rootView.findImageView(resId)

    fun listView(resId: Int): ListView = rootView.findListView(resId)

    fun textView(resId: Int): TextView = rootView.findTextView(resId)

    fun editText(resId: Int): EditText = rootView.findEditText(resId)

    fun switch(resId: Int): Switch = rootView.findViewById(resId)
    fun progress(resId: Int): ProgressBar = rootView.findViewById(resId)

    fun view(resId: Int): View = rootView.findViewById(resId)
    fun spinner(resId: Int): Spinner = rootView.findViewById(resId)
    fun checkBox(resId: Int): CheckBox = rootView.findViewById(resId)
    fun recyclerView(resId:Int):RecyclerView = rootView.findRecyclerView(resId)
}

fun Context.createDialog(layoutId: Int, action: SDialog.() -> Unit): SDialog {
    val model = SDialog(this, layoutId)
    action(model)
    return model
}

fun Fragment.createDialog(layoutId: Int, action: SDialog.() -> Unit): SDialog {
    return requireContext().createDialog(layoutId, action)
}